from task_service import AbsTaskService, XpcContext
from task_service.task_context import EquInfo, WorkStationInfo, StorerDeliverLayer
from x_pc.service import solve


class XPCTaskService(AbsTaskService):
    """
    排程
    """
    name: str = 'PC'

    all_equ_info: list[EquInfo] = []
    work_station_info: list[WorkStationInfo] = []
    storer_layer_dict: dict[str, StorerDeliverLayer] = {}

    def _pre_process(self):
        if self.context.allEquInfo:
            self.all_equ_info = [EquInfo(**_) for _ in self.context.allEquInfo]
        if self.context.workStationInfo:
            self.work_station_info = [WorkStationInfo(**_) for _ in self.context.workStationInfo]
        if self.context.storerList:
            storer_dict = {}
            for s in self.context.storerList:
                storer_dict[s.deliver_code] = s
            self.storer_layer_dict = storer_dict

    def _process(self):
        ctx: XpcContext = self.context
        result = solve(ctx.deliveryTaskIdList, ctx.interiorTsakIdList, ctx.preworkTsakIdList, ctx.tallyTsakIdList,
                       ctx.pendingTaskIdList, self.all_equ_info, self.work_station_info, ctx.baseDateTime,
                       self.storer_layer_dict, ctx.app.logger)
        ctx.result = result.to_dict()
